﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.CustomerService.BLL;
using Koonsoft.CustomerService.Entity;

namespace CustomerService.Issues
{
    public partial class IssuesEdit : PageBase
    {
        private Issue issue;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Guid guid = Guid.Empty;
                try
                {
                    guid = Guid.Parse(Request["Guid"].ToString());

                    string msg;
                    issue = new IssuesBLL().GetIssues(new Issue() { IssueID = guid, Status = -1 }, out msg).FirstOrDefault();


                    //labClientVersionValue.Text = issue.ClientVersion;
                    labCreateUser.Text = issue.CreateUser;
                    txtCustomersMobile.Text = issue.CustomersMobile;
                    txtCustomersQQ.Text = issue.CustomersQQ;
                    txtDecodingRate.Text = issue.DecodingRate;
                    labExceptionTime.Text = issue.ExceptionTime.ToString("yyyy-MM-dd HH:mm");
                    // labIssueCategory.Text = issue.IssueCategory;
                    //labIssueLevel.Text = issue.IssueLevel;
                    // labIssueSource.Text = issue.IssueSource;
                    txtIssueTitle.Text = issue.IssueTitle;
                    if (issue.IsUnblocked == true)
                    {
                        cbIsUnblocked.Checked = true;
                    }
                    else
                    {
                        cbIsUnblocked.Checked = false;
                    }

                    txtRegion.Text = issue.Region;
                    //labServiceArea.Text = issue.ServiceArea;
                    labStatus.Text = new WFBLL().GetWfById(issue.Status).State;

                    //labBandWidth.Text = string.Format("{0} {1}", issue.OperatorsId, issue.BandWidth);
                    txtUserName.Text = issue.UserName;
                    txtUserAccounts.Text = issue.UserAccounts;
                    txtGSID.Text = issue.GSID;
                    labId.Text = issue.ID;
                    //labPrincipal.Text = issue.PrincipalID;

                    LabIssueID.Text = guid.ToString();


                    txtMark.Text = issue.Mark.Replace("\n", "<br />");

                    List<IssueAccessorie> issueAccessories = new IssueAccessoriesBLL().GetIssueAccessorie(guid, out msg);

                    if (issueAccessories != null && issueAccessories.Count > 0)
                    {
                        StringBuilder sbAccessories = new StringBuilder();
                        sbAccessories.Append("已上传附件:");
                        foreach (var acc in issueAccessories)
                        {
                            sbAccessories.AppendFormat("<div><a href='{1}'>{0}</a></div>", acc.AccessoryName, acc.Accessory);
                        }
                        labAccessories.Text = sbAccessories.ToString().Replace("~/", "../");
                    }
                }
                catch
                {
                    AlterCloseReturn("GUID错误", "");
                }
            }
        }

        protected void dropIssueLevel_DataBound(object sender, EventArgs e)
        {
            if (issue != null)
            {
                try
                {
                    dropIssueLevel.Items.FindByText(issue.IssueLevel).Selected = true;
                }
                catch
                {
                }
            }
        }

        protected void dropIssueCategory_DataBound(object sender, EventArgs e)
        {
            if (issue != null)
            {
                try
                {
                    dropIssueCategory.Items.FindByText(issue.IssueCategory).Selected = true;
                }
                catch
                {
                }
            }
        }

        protected void dropIssueSource_DataBound(object sender, EventArgs e)
        {
            if (issue != null)
            {
                try
                {
                    dropIssueSource.Items.FindByText(issue.IssueSource).Selected = true;
                }
                catch
                {
                }
            }
        }

        protected void dropClientVersion_DataBound(object sender, EventArgs e)
        {
            if (issue != null)
            {
                try
                {
                    dropClientVersion.Items.FindByText(issue.ClientVersion).Selected = true;
                }
                catch
                {
                }
            }
        }

        protected void dropOperators_DataBound(object sender, EventArgs e)
        {
            if (issue != null)
            {
                try
                {
                    dropOperators.Items.FindByText(issue.OperatorsId).Selected = true;
                }
                catch
                {
                }
            }
        }

        protected void dropBandWidth_DataBound(object sender, EventArgs e)
        {
            if (issue != null)
            {
                try
                {
                    dropBandWidth.Items.FindByText(issue.BandWidth).Selected = true;
                }
                catch
                {
                }
            }
        }

        protected void dropServiceArea_DataBound(object sender, EventArgs e)
        {
            if (issue != null)
            {
                try
                {
                    dropServiceArea.Items.FindByText(issue.ServiceArea).Selected = true;
                }
                catch
                {
                }
            }
        }

        protected void dropPrincipal_DataBound(object sender, EventArgs e)
        {
            if (issue != null)
            {
                try
                {
                    dropPrincipal.Items.FindByText(issue.PrincipalID).Selected = true;
                }
                catch
                {
                }
            }
        }

        protected void dropSystemEnvironment_DataBound(object sender, EventArgs e)
        {
            if (issue != null)
            {
                try
                {
                    dropSystemEnvironment.Items.FindByText(issue.SystemEnvironment).Selected = true;
                }
                catch
                {
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtIssueTitle.Text))
            {
                AlertMsg("请填写标题");
                return;
            }

            if (string.IsNullOrEmpty(txtCustomersQQ.Text)
                    && string.IsNullOrEmpty(txtCustomersMobile.Text))
            {
                AlertMsg("请填写客户QQ或电话，保证可以正常回访");
                return;
            }
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
            string filePathBase = "~/Upload/";

            List<IssueAccessorie> accessorie = new List<IssueAccessorie>();
            Guid guid = Guid.Parse(LabIssueID.Text);

            for (int ifile = 0; ifile < files.Count; ifile++)
            {
                if (files[ifile].FileName.Length > 0)
                {
                    string fileName = Server.UrlDecode(files[ifile].FileName);
                    string filePath = filePathBase + DateTime.Now.ToString("yyyyMMddHHmmss") + fileName;
                    //上传单个文件并保存相关信息
                    System.Web.HttpPostedFile myFile = files[ifile];
                    myFile.SaveAs(Page.Server.MapPath(filePath));

                    IssueAccessorie acc = new IssueAccessorie();
                    acc.IssueId = guid;
                    acc.Accessory = filePath;
                    acc.AccessoryName = fileName;

                    accessorie.Add(acc);
                }
            }


            Issue issueNew = new Issue();
            issueNew.ID = labId.Text.Trim();
            issueNew.IssueID = guid;
            issueNew.IssueTitle = txtIssueTitle.Text;
            issueNew.IssueLevel = dropIssueLevel.SelectedValue;
            issueNew.IssueCategory = dropIssueCategory.SelectedValue;
            issueNew.IssueSource = dropIssueSource.SelectedValue;
            issueNew.ClientVersion = dropClientVersion.SelectedValue;
            issueNew.CustomersQQ = txtCustomersQQ.Text;
            issueNew.CustomersMobile = txtCustomersMobile.Text;
            issueNew.ServiceArea = dropServiceArea.SelectedValue;
            issueNew.Region = txtRegion.Text.Trim();
            issueNew.BandWidth = dropBandWidth.SelectedValue;
            issueNew.SystemEnvironment = dropSystemEnvironment.SelectedValue;
            issueNew.DecodingRate = txtDecodingRate.Text;
            issueNew.IsUnblocked = cbIsUnblocked.Checked;
            issueNew.Mark = txtMark.Text;
            issueNew.CreateUser = LoginName;
            issueNew.UserName = txtUserName.Text.Trim();
            issueNew.UserAccounts = txtUserAccounts.Text.Trim();
            issueNew.GSID = txtGSID.Text.Trim();
            issueNew.PrincipalID = dropPrincipal.SelectedValue;
            issueNew.OperatorsId = dropOperators.SelectedValue;



            string msg = "";
            if(new IssuesBLL().IssuesUpdate(issueNew,accessorie,out msg))
            {
                AlterAndRedirect("修改成功", "../Default.aspx");
            }
            else
            {
                AlertMsg("修改失败，请联系管理员：" + msg.Split(';'));
            }
            //new IssuesBLL().Create(issue, accessorie, out msg);
            //if (string.IsNullOrEmpty(msg))
            //{
            //    AlterAndRedirect("创建成功", "../Default.aspx");
            //}
            //else
            //{
            //    AlertMsg("创建失败，请联系管理员：" + msg.Split(';'));
            //}
        }
    }
}